const UpDateData = {
    main(){
        let elem = AddData.getElement();
        this.setFronData(elem);
    },

    setFronData(elem){
        // 定义一个获取数组下标
        let key;

        const J_Data = document.getElementById('J_Data');

        J_Data.addEventListener('click', (e) => {
            if(e.target.tagName === 'LI'){
                let list = e.target.parentElement.children;
                elem.J_title.innerHTML = '修改员工信息';
                

                // 根据id查找下标
                for(let index in Data){
                    if(Data[index].userId == list[1].innerText){
                        key = index;
                    };
                }

                elem.userId.value = Data[key].userId;
                elem.userName.value = Data[key].userName;
                if(Data[key].userSex === '男'){
                    elem.userSex[0].checked = true;
                }else{
                    elem.userSex[1].checked = true;
                }
                elem.userAge.value = Data[key].userAge;
                elem.userPhone.value = Data[key].userPhone;
                elem.userDescript.value = Data[key].userDescript;

                let arr1 = Data[key].userBirth;
                arr1 = arr1.split('/');
                arr1 = UpDateData.getBirth(arr1);
                Renderer.birth(arr1);


                let arr2 = Data[key].userAddress;
                arr2 = arr2.split('/');
                arr2 = UpDateData.dataSelect(arr2,data);
                Renderer.cityFun(data, arr2);
        
                J_model.style.display = 'block';

                

                AddData.submitData(key);
            }
        });
    },

    dataSelect(arr,data){
        let arrry = [];
        if(arr.length > 0){
            for(let i = 0; i < data.length; i++){
                if(data[i]['n'] == arr[0]){
                    arrry[0] = i;
                }
                if(arr.length > 1){
                    for(let j = 0; j < data[i]['c'].length; j++){
                        if(data[i]['c'][j]['n'] == arr[1]){
                            arrry[1] = j;
                        }
                        if(arr.length > 2){
                            for(let k = 0; k < data[i]['c'][j]['c'].length; k++){
                                if(data[i]['c'][j]['c'][k]['n'] == arr[2]){
                                    arrry[2] = k;
                                }
                            }
                        }
                    }
                }
            }
        }
        return arrry;
    },

    getBirth(arr){
        let J_year = document.getElementById('J_year').children;
        let J_month = document.getElementById('J_month').children;
        let J_day = document.getElementById('J_day').children;

        if(arr.length > 0){
            for(let i = 0; i < J_year.length; i++){
                if(J_year[i].value == arr[0]){
                    arr[0] = i;
                }
            }
        }

        if(arr.length > 1){
            for(let i = 0; i < J_month.length; i++){
                if(J_month[i].value == arr[1]){
                    arr[1] = i;
                }
            }
        }

        if(arr.length > 2){
            for(let i = 0; i < J_day.length; i++){
                if(J_day[i].value == arr[2]){
                    arr[2] = i;
                }
            }
        }
        return arr;
    }
}